/* eslint-disable @iceworks/best-practices/recommend-polyfill */
/**
 * 封装腾讯地图API
 */

// const base = 'https://apis.map.qq.com/ws';
const base = '/lbs';
const key = '5M6BZ-GS2KJ-SOLFY-FCORA-7Q34E-STF6C';

/**
 * 坐标逆解析, 用于获取坐标所属行政区代码
 * @param {{
 *  lat: string,  // 纬度
 *  lng: string   // 经度
 * }} param0
 * @returns
 * ```
 * {
  "status": 0,
  "message": "query ok",
  "request_id": "c73d0999-30f8-4a51-bef4-dfcb22bef213",
  "result": {
    "location": {
      "lat": 22.534576,
      "lng": 113.973015
    },
    "address": "广东省深圳市南山区欧路休闲街",
    "formatted_addresses": {
      "recommend": "南山区华侨城恺撒宫(欧路休闲街南)",
      "rough": "南山区华侨城恺撒宫(欧路休闲街南)"
    },
    "address_component": {
      "nation": "中国",
      "province": "广东省",
      "city": "深圳市",
      "district": "南山区",
      "street": "欧路休闲街",
      "street_number": "欧路休闲街"
    },
    "ad_info": {
      "nation_code": "156",
      "adcode": "440305",
      "phone_area_code": "0755",
      "city_code": "156440300",
      "name": "中国,广东省,深圳市,南山区",
      "location": {
        "lat": 22.533191,
        "lng": 113.930478
      },
      "nation": "中国",
      "province": "广东省",
      "city": "深圳市",
      "district": "南山区"
    },
    "address_reference": {
      "street_number": {
        "id": "",
        "title": "",
        "location": {
          "lat": 22.535849,
          "lng": 113.972878
        },
        "_distance": 55,
        "_dir_desc": "南"
      },
      "business_area": {
        "id": "11442391563743059603",
        "title": "华侨城",
        "location": {
          "lat": 22.5375,
          "lng": 113.981
        },
        "_distance": 0,
        "_dir_desc": "内"
      },
      "famous_area": {
        "id": "11442391563743059603",
        "title": "华侨城",
        "location": {
          "lat": 22.5375,
          "lng": 113.981
        },
        "_distance": 0,
        "_dir_desc": "内"
      },
      "crossroad": {
        "id": "1261827",
        "title": "侨城西街/深南大道(路口)",
        "location": {
          "lat": 22.53645,
          "lng": 113.97618
        },
        "_distance": 380.6,
        "_dir_desc": "西南"
      },
      "town": {
        "id": "440305003",
        "title": "沙河街道",
        "location": {
          "lat": 22.536512,
          "lng": 113.979491
        },
        "_distance": 0,
        "_dir_desc": "内"
      },
      "street": {
        "id": "753631269670772314",
        "title": "欧路休闲街",
        "location": {
          "lat": 22.535849,
          "lng": 113.972878
        },
        "_distance": 55,
        "_dir_desc": "南"
      },
      "landmark_l2": {
        "id": "18154546062098747686",
        "title": "恺撒宫",
        "location": {
          "lat": 22.535016,
          "lng": 113.973375
        },
        "_distance": 18.6,
        "_dir_desc": ""
      }
    }
  }
}
 * ```
 */
export async function geoCoder({ lat, lng }) {
  return fetch(`${base}/geocoder/v1/?key=${key}&location=${lat},${lng}`).then((r) => r.json());
}

/**
 * 返回坐标所属三级行政区划代码
 * @param {object} param0
* ```
* {
*   lat: string,  // 纬度
*   lng: string   // 经度
* }
* ```
* @returns string 行政区划代码
 */
export async function adcode({ lat, lng }) {
  const res = await geoCoder({ lat, lng });
  // return res.result.address_reference.town.id
  //     || res.result.ad_info.ad_info;
  return res.result.ad_info.adcode;
}

/**
 *
 * @param {string} district 城市名，行政区名等
 * @returns
 * ```
 * {
 *   adcode: string,  // 行政区划代码
 *   lat: string,     // 维度
 *   lng: string,     // 经度
 * }
 * ```
 */
export async function districtGeo(district) {
  const res = await fetch(`${base}/district/v1/search?key=${key}&keyword=${district}`).then((r) => r.json());
  // https://lbs.qq.com/service/webService/webServiceGuide/webServiceDistrict#6
  const info = res.result?.[0]?.[0];
  return {
    adcode: info?.id,
    lat: info?.location.lat,
    lng: info?.location.lng,
  };
}

/**
 *
 * @param {string} address 地址名，用城市开头，如深圳市南山区科技园
 * @returns
 * ```
 * {
 *   adcode: string,  // 行政区划代码
 *   lat: string,     // 维度
 *   lng: string,     // 经度
 * }
 * ```
 */
export async function addressGeo(address) {
  const res = await fetch(`${base}/geocoder/v1/?key=${key}&address=${address}`).then((r) => r.json());
  // https://lbs.qq.com/service/webService/webServiceGuide/webServiceGeocoder
  const { result } = res;
  return {
    adcode: result?.ad_info?.adcode,
    lat: result?.location?.lat,
    lng: result?.location?.lng,
  };
}

/**
 *
 * @param {string} adcode   行政区划代码
 * @param {string} keyword  关键字
 * @returns
 * ```
 * {
 *   "count": 946,
 *   "data": [
 *     {
 *       "id": "10056530351165939652",
 *       "title": "Action X",
 *       "address": "香港特别行政区中西区上环文咸东街28号",
 *       "tel": "+85231022977",
 *       "category": "购物:服饰鞋包:服装",
 *       "type": 0,
 *       "location": {
 *         "lat": 22.282921,
 *         "lng": 114.157071
 *       },
 *       "ad_info": {
 *         "adcode": 811100,
 *         "province": "香港特别行政区",
 *         "city": "中西区",
 *         "district": "中西区"
 *       }
 *     }
 *   ]
 * }
 * ```
 */
export async function search(adCode, keyword) {
  // https://lbs.qq.com/service/webService/webServiceGuide/webServiceSearch#3
  // region(adCode,0)，仅在指定行政区内搜索
  const res = await fetch(`${base}/place/v1/search?key=${key}&get_subpois=0&keyword=${keyword}&boundary=region(${adCode},0)`).then((r) => r.json());
  return res;
}

